package com.xjtu.conan.controller;


import com.xjtu.conan.entity.ArticleCommentsInfo;
import com.xjtu.conan.entity.UserInfo;
import com.xjtu.conan.response.AjaxResponse;
import com.xjtu.conan.service.ArticleCommentsInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;

/**
 * <p>
 * 话题评论表 前端控制器
 * </p>
 *
 * @author 一只黑猩猩
 * @since 2021-04-19
 */
@CrossOrigin
@RestController
@RequestMapping("/conan/article-comments-info")
@Api(value = "话题对应评论管理模块", description = "这是话题对应评论信息CRUD的模块")
public class ArticleCommentsInfoController {
    @Autowired
    private ArticleCommentsInfoService commentsInfoService;

    @ApiOperation(value = "获取全部话题评论", notes = "获取所有话题的评论信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "current", value = "当前页", required = false, defaultValue = "1", paramType = "query", dataType = "int"),
            @ApiImplicitParam(name = "size", value = "每页数量", required = false, defaultValue = "10", paramType = "query", dataType = "int")
    })
    @RequestMapping(value = "/getAllComments", method = RequestMethod.GET)
    public AjaxResponse getAllComments(@RequestParam(value = "current") Integer current,
                                       @RequestParam(value = "size") Integer size) {

        return AjaxResponse.success(commentsInfoService.getAllArticleCommentsInfo(current, size));
    }

    @ApiOperation(value = "添加话题评论", notes = "为某一个话题添加评论")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "content", value = "评论内容", required = true, paramType = "form", dataType = "String"),
            @ApiImplicitParam(name = "aid", value = "所属话题id", required = true, paramType = "form", dataType = "Long"),
            @ApiImplicitParam(name = "pid", value = "所属父评论id", required = true, defaultValue = "-1", paramType = "form", dataType = "Long")
    })
    @RequestMapping(value = "addArticleComments", method = RequestMethod.POST)
    public AjaxResponse addArticleComments(@RequestParam(value = "content") String content,
                                        @RequestParam(value = "aid") Long aid,
                                        @RequestParam(value = "pid") Long pid,
                                        HttpServletRequest request) throws IOException {
        UserInfo loginUser = (UserInfo) request.getSession().getAttribute("loginUser");
        ArticleCommentsInfo commentsInfo = new ArticleCommentsInfo()
                .setArticleId(aid)
//                .setUserId(loginUser.getUid())
                .setUserId(1384414001958338562L)
                .setNcContent(content)
                .setParentId(pid);
        return AjaxResponse.success("新增评论ID->" + commentsInfoService.addArticleComments(commentsInfo));
    }

    @ApiOperation(value = "更新话题评论", notes = "录入新的话题评论信息并且修改")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "content", value = "评论内容", required = true, paramType = "form", dataType = "String"),
            @ApiImplicitParam(name = "aid", value = "所属话题id", required = true, paramType = "form", dataType = "Long"),
            @ApiImplicitParam(name = "pid", value = "所属父评论id", required = true, defaultValue = "-1", paramType = "form", dataType = "Long")
    })
    @RequestMapping(value = "updateArticleComments", method = RequestMethod.PUT)
    public AjaxResponse updateArticleComments(@RequestParam(value = "content") String content,
                                           @RequestParam(value = "aid") Long aid,
                                           @RequestParam(value = "pid") Long pid,
                                           HttpServletRequest request) throws IOException {
        UserInfo loginUser = (UserInfo) request.getSession().getAttribute("loginUser");
        ArticleCommentsInfo commentsInfo = new ArticleCommentsInfo()
                .setArticleId(aid)
//                .setUserId(loginUser.getUid())
                .setUserId(1384414001958338562L)
                .setNcContent(content)
                .setParentId(pid);
        return AjaxResponse.success(commentsInfoService.updateArticleComments(commentsInfo));
    }

    @ApiOperation(value = "删除话题评论信息", notes = "从数据库中删除某个话题")
    @ApiImplicitParam(name = "acid", value = "删除话题id", required = true, paramType = "query", dataType = "Long")
    @RequestMapping(value = "/deleteArticleComments", method = RequestMethod.DELETE)
    public AjaxResponse deleteArticleComments(@RequestParam(value = "acid") Long acid) {
        return AjaxResponse.success("数据库受影响行数->" + commentsInfoService.deleteArticleComments(acid));
    }

    @ApiOperation(value = "根据父评论id查找话题评论", notes = "获取根据父评论id查找到的话题评论")
    @ApiImplicitParam(name = "pid", value = "父评论id", required = true, paramType = "query", dataType = "Long")
    @RequestMapping(value = "/getArticleCommentsBypId", method = RequestMethod.GET)
    public AjaxResponse getArticleCommentsBypId(@RequestParam(name = "pid") Long pid) {
        return AjaxResponse.success(commentsInfoService.getArticleCommentsBypid(pid));
    }

    @ApiOperation(value = "根据话题评论id查找话题评论", notes = "获取根据评论id查找到的话题评论")
    @ApiImplicitParam(name = "acid", value = "评论id", required = true, paramType = "query", dataType = "Long")
    @RequestMapping(value = "/getArticleCommentsByacId", method = RequestMethod.GET)
    public AjaxResponse getArticleCommentsByacId(@RequestParam(name = "acid") Long acid) {
        return AjaxResponse.success(commentsInfoService.getArticleCommentByid(acid));
    }

    @ApiOperation(value = "根据话题id查找对应评论", notes = "获取根据话题id查找到的话题评论")
    @ApiImplicitParam(name = "aid", value = "话题id", required = true, paramType = "query", dataType = "Long")
    @RequestMapping(value = "/getArticleCommentsByaid", method = RequestMethod.GET)
    public AjaxResponse getArticleCommentsByaid(@RequestParam(name = "aid") Long aid) {
        return AjaxResponse.success(commentsInfoService.getArticleCommentsByaid(aid));
    }

    @ApiOperation(value = "话题评论总数量", notes = "获取数据库中话题评论总数量")
    @RequestMapping(value = "/getTotal", method = RequestMethod.GET)
    public AjaxResponse getTotal() {
        return AjaxResponse.success(commentsInfoService.getTotal());
    }
}

